//
//  TollgateScene.cpp
//  LitterRunner
//
//  Created by 李阳 on 15/11/20.
//
//

#include "TollgateScene.hpp"
USING_NS_CC;
#include "cocos-ext.h"
#include "MonsterManager.hpp"
using namespace cocos2d::extension;

CCScene *TollgateScene::scene(){
    CCScene *scene = CCScene::create();
    TollgateScene *layer = TollgateScene::create();
    scene->addChild(layer);
    return scene;
}

bool TollgateScene::init(){
    bool bRet = false;
    do {
        CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
        
        CCLabelTTF *titlabel = CCLabelTTF::create("小猴纸!你到是跳啊！", "Arial", 50);
        titlabel->cocos2d::CCNode::setPosition(visibleSize.width/2, visibleSize.height-50);
        titlabel->setColor(ccc3(1, 0.1, 0.1));
        this->addChild(titlabel,2);
        
        m_player = Player::create();
        m_player->bindSprite(CCSprite::create("monkey.png"));
        m_player->setPosition(ccp(visibleSize.width/2, visibleSize.height/4+20));
        m_player->setScale(0.3);
        this->addChild(m_player,2);
        
        CCLabelTTF *jumpTitle = CCLabelTTF::create("Jump", "Arial", 30);
        CCScale9Sprite *norBG = CCScale9Sprite::create("CloseSelected.png");
        CCControlButton *jumpBtn = CCControlButton::create(jumpTitle,norBG);
        jumpBtn->setPosition(visibleSize.width-80, 50);
        jumpBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(TollgateScene::jumpEvent), CCControlEventTouchUpInside);
        this->addChild(jumpBtn,4);
        
        initBG();
        
        this->scheduleUpdate();
        
        MonsterManager *monsterMgr = MonsterManager::create();
        this->addChild(monsterMgr,4);
        
        bRet = true;
    } while (0);
    return bRet;
}
void TollgateScene::initBG(){
    CCSize visibleSize1 = CCDirector::sharedDirector()->getVisibleSize();
    m_bgSprite1 = CCSprite::create("background_1.jpg");
    m_bgSprite1->setPosition(ccp(visibleSize1.width/2, visibleSize1.height/2));
    this->addChild(m_bgSprite1,0);
    
    m_bgSprite2 = CCSprite::create("background_1.jpg");
    m_bgSprite2->setPosition(ccp(visibleSize1.width*3/2, visibleSize1.height/2));
    m_bgSprite2->setFlipX(true);//水平翻转精灵
    this->addChild(m_bgSprite2,0);
}
void TollgateScene::update(float delta){
    
    float posX1 = m_bgSprite1->getPositionX();
    float posX2 = m_bgSprite2->getPositionX();
    float iSpeed = 1.0;
    posX1 -= iSpeed;
    posX2 -= iSpeed;
    
    CCSize mapSize = m_bgSprite1->getContentSize();
    if (posX1 == mapSize.width/2) {
        posX2 = mapSize.width*3/2;
    }
    if (posX2 == mapSize.width/2) {
        posX1 = mapSize.width*3/2;
    }
    
    m_bgSprite1->setPositionX(posX1);
    m_bgSprite2->setPositionX(posX2);
}
void TollgateScene::jumpEvent(cocos2d::CCObject *pSender, cocos2d::extension::CCControlEvent *event){
    m_player->jump();
}












